//
//  UITableView+FCEmpTableViewSet.m
//  AFNetworking
//
//  Created by WJC on 2019/6/10.
//

#import "UITableView+FCEmpTableViewSet.h"
#import <objc/runtime.h>
#import "FCEmptyView.h"
#import "WJCDefine.h"
#import "UIView+returnBlock.h"
@implementation UITableView (FCEmpTableViewSet)
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method reloadData    = class_getInstanceMethod(self, @selector(reloadData));
        Method emp_reloadData = class_getInstanceMethod(self, @selector(emp_reloadData));
        method_exchangeImplementations(reloadData, emp_reloadData);
    });
}
-(void)emp_reloadData{
    dispatch_async(dispatch_get_main_queue(), ^{
        
   
    [self emp_reloadData];
     if (self.setEmptyView) {
    NSInteger numberOfSections = [self numberOfSections];
    BOOL havingData = NO;
    for (NSInteger i = 0; i < numberOfSections; i++) {
        if ([self numberOfRowsInSection:i] > 0) {
            havingData = YES;
        }
    }
    if (!havingData) {
        NSString *title = [NSString stringWithFormat:@"%@",self.title?self.title:@"暂无数据"];
            NSString *imageName = @"ic_interface_lost";
//        static  FCEmptyView * emptyView;
         FCEmptyView *emptyView = [FCEmptyView emptyViewWithTitle:title imageName:self.emptyImg?self.emptyImg:imageName btnTitle:self.buttonTitle ? self.buttonTitle:@"" target:self action:@selector(returnClick)];
        
            self.ly_emptyView = emptyView;
            [self ly_showEmptyView];
    }else{
          [self ly_hideEmptyView];
    }
     }
    });
}
-(void)returnClick{
    if (self.voidBlock) {
        self.voidBlock();
    }
}
-(void)setButtonTitle:(NSString *)buttonTitle{
    return objc_setAssociatedObject(self,@selector(buttonTitle),buttonTitle,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)buttonTitle{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setTitle:(NSString *)title{
    return objc_setAssociatedObject(self,@selector(title),title,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)title{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setSetEmptyView:(BOOL)setEmptyView{
     return objc_setAssociatedObject(self,@selector(setEmptyView),@(setEmptyView),OBJC_ASSOCIATION_ASSIGN);
}
-(BOOL)setEmptyView{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setEmptyImg:(UIImage *)emptyImg{
    return objc_setAssociatedObject(self,@selector(emptyImg),emptyImg,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImage *)emptyImg{
     return objc_getAssociatedObject(self, _cmd);
}
@end
